package com.ingenio.siot.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.ingenio.siot.adapter.ParametroAdapter;
import com.ingenio.siot.entity.Parametro;

/**
 * Servlet implementation class ABMParametroServlet
 */
@WebServlet("/ABMParametro")
public class ABMParametroServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ABMParametroServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ParametroAdapter pa = new ParametroAdapter();
		Parametro p = null;
		JSONObject jo = new JSONObject();
		if("editar".equals(request.getParameter("type"))){
			if(true){
				String param = request.getParameter("param");
				try {
					jo.put("action", "editar");
					p = pa.devolverParametro(param);
					String valor = request.getParameter("valor");
					p.setValor(valor);
					pa.editarValorParametro(p);
					jo.put("param", p.getParametro());
					jo.put("valor", p.getValor());
					jo.put("error", false);
				} 
				catch (Exception e1) {
					try {
						jo.put("error", true);
					} catch (JSONException e) {
						e.printStackTrace();
					}
					e1.printStackTrace();
				}
			}
		}
		response.setContentType("application/json");
		response.getWriter().write(jo.toString());
	}
	
}
